<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
<div id="app"></div>

<!-- react核心库 -->
<script src="../lib/react.development.js"></script>
<!-- react操作dom -->
<script src="../lib/react-dom.development.js"></script>
<!-- jsx转js -->
<script src="../lib/babel.min.js"></script>
<script type="text/babel">
  class MyComponent extends React.Component {
    constructor(props) {
      super(props)
      // 初始化状态
      this.state = {
        like: 0,
        name: 'admin'
      }
      // 绑定事件，解决事件的this指向问题
      this.addLike1 = this.addLike.bind(this)
    }

    render() {
      console.log(this)   // MyComponent实例
      const {like} = this.state
      console.log(like)
      return (
        <div>
          <div>用户名： {this.state.name}</div>
          <div>点赞数：{this.state.like}</div>
          <button onClick={this.addLike1}>点赞</button>
        </div>
      )
    }

    addLike() {
      /*
      constructor里没有绑定事件的时候，onClick里绑定的addLike是给浏览器回调用的，
      没有实例对象，所以this是undefined
      所以需要this.addLike.bind(this)，改变addLike的this指向
       */
      console.log(this)
      // react禁止直接更改状态值，需通过setState() API
      this.setState({like: ++this.state.like})
    }
  }

  ReactDOM.render(<MyComponent/>, document.getElementById("app"))
</script>
</body>
</html>